home *** CD-ROM | disk | FTP | other *** search
- package com.extensibility.util;
-
- import java.util.Hashtable;
- import java.util.Vector;
-
- public class LRUCache {
- protected static final int DEFAULT_MAX_SIZE = 32;
- Vector history;
- Hashtable map;
- int maxSize;
-
- public LRUCache() {
- this(32);
- }
-
- public LRUCache(int var1) {
- this.history = new Vector(var1);
- this.map = new Hashtable(var1 * 2 + 1);
- this.maxSize = var1;
- }
-
- public Object get(Object var1) {
- Object var2 = this.map.get(var1);
- if (var1 != null) {
- this.history.removeElement(var1);
- this.history.addElement(var1);
- }
-
- return var2;
- }
-
- public Object put(Object var1, Object var2) {
- Object var3 = null;
- if (this.map.contains(var1)) {
- var3 = this.put(var1, var2);
- this.history.removeElement(var1);
- } else if (this.history.size() == this.maxSize) {
- this.map.remove(this.history.elementAt(0));
- this.history.removeElementAt(0);
- }
-
- this.history.addElement(var1);
- this.map.put(var1, var2);
- return var3;
- }
-
- public void remove(Object var1) {
- this.map.remove(var1);
- this.history.removeElement(var1);
- }
-
- public void replaceKey(Object var1, Object var2) {
- Object var3 = this.get(var1);
- if (var3 != null) {
- this.put(var2, var3);
- this.remove(var1);
- }
-
- }
- }
-